package com.web.exiaodao.remote.controller;

import com.web.exiaodao.remote.service.ApiAutherWorksTableService;
import com.web.exiaodao.remote.vo.ArtInformationVo;
import com.web.exiaodao.remote.vo.AutherWorksTableVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Title: ApiAutherWorksTableController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 玩/艺术/学乐/达人-作品表信息接口
 * @author wangxuemei  
 * @date 2016-12-14 上午10:22:39
 * @version V1.0
 */
@Api(value = "AutherWorksTable", description = "玩/艺术/学乐/达人-作品表接口", position = 10)
@Controller
@RequestMapping("/api/autherWorksTable")
public class ApiAutherWorksTableController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ApiAutherWorksTableController.class);

	@Autowired
	private ApiAutherWorksTableService autherWorksTableService;
	
	/**
	 * @Title: findAutherWorksTableList
	 * @Description: 查询所有玩/艺术/学乐/达人-作品表信息
	 * @return    
	 */
	@ApiOperation(value="玩乐列表接口",notes="查询所有玩/艺术/学乐/达人信息,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value="findAutherWorksTableList",method=RequestMethod.POST)
	public XaResult<Page<AutherWorksTableVo>> findAutherWorksTableList(
		@ApiParam("类型,字段名:type,1:作品列表 2：作者列表") @RequestParam(defaultValue = "1" ) Integer type,	
		@ApiParam("作者名称,字段名:authorName") @RequestParam(value = "authorName", required=false) String authorName,
		@ApiParam("作品名称,字段名:worksName") @RequestParam(value = "worksName", required=false) String worksName,
		@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
		@ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
	) throws BusinessException{
        XaResult<Page<AutherWorksTableVo>> xr = new  XaResult<Page<AutherWorksTableVo>>();
		Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'sort',direction:'ASC'},{property:'clickCont',direction:'DESC'},{property:'createTime',direction:'DESC'}]");

		try{
			AutherWorksTableVo autherWorksTableVo = new AutherWorksTableVo();
			autherWorksTableVo.setWorksName(worksName);
			ArtInformationVo v = new ArtInformationVo();
			v.setAuthorName(authorName);
			autherWorksTableVo.setArtInformationVo(v);
			return autherWorksTableService.findAutherWorksTableList(type,pageable,autherWorksTableVo);
		}catch(Exception e){
			if (e instanceof BusinessException) {
				xr.error(e.getMessage(), true);
			} else  {
				xr.error(XaConstant.Message.error, true);
			}
			LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}

	
	/**
	 * @Title: findAutherWorksTableById
	 * @Description: 根据ID查找单条实体
	 * //@param modelId
	 * @return    
	 */
	@ApiOperation(value="玩乐详情接口",notes="查询玩/艺术/学乐/达人-作品表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findAutherWorksTableById",method=RequestMethod.POST)
	public XaResult<List<AutherWorksTableVo>> findAutherWorksTableById(
		@ApiParam("类型,字段名:type,1:作品列表 2：作者列表") @RequestParam(defaultValue = "1" ) Integer type,	
		@ApiParam("用户ID,字段名:userId") @RequestParam(value = "userId") Long userId,
		@ApiParam("作品ID,字段名:worksId") @RequestParam(value = "worksId", required=false) Long worksId,
		@ApiParam("作者ID,字段名:artId") @RequestParam(value = "artId", required=false) Long artId
	) throws BusinessException{
		XaResult<List<AutherWorksTableVo>> xr = new  XaResult<List<AutherWorksTableVo>>();
		try{
			AutherWorksTableVo autherWorksTableVo = new AutherWorksTableVo();
			autherWorksTableVo.setId(worksId);
			autherWorksTableVo.setAutherID(artId);
			return autherWorksTableService.findAutherWorksTableById(type,userId,autherWorksTableVo);
		}catch(Exception e){
			if (e instanceof BusinessException) {
						 xr.error(e.getMessage());
			} else  {
				 xr.error(XaConstant.Message.error);
			}
			 LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}
	
	@ApiOperation(value="查询主要作品",notes="查询玩/艺术/学乐/达人-作品表详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findWorksById",method=RequestMethod.POST)
	public XaResult<List<AutherWorksTableVo>> findWorksById(
		@ApiParam("作者ID,字段名:atrId") @RequestParam(value = "atrId") Long atrId
	) throws BusinessException{
		XaResult<List<AutherWorksTableVo>> xr = new  XaResult<List<AutherWorksTableVo>>();
		try{
			return autherWorksTableService.findWorksById(atrId);
		}catch(Exception e){
			if (e instanceof BusinessException) {
						 xr.error(e.getMessage());
			} else  {
				 xr.error(XaConstant.Message.error);
			}
			 LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}
	
}

